package com.ct.gd.passport.view.consult;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.BaseActivity;
import com.ct.gd.passport.common.constants.UmengVar;
import com.ct.gd.passport.domain.AdviceForm;
import com.ct.gd.passport.view.BundleKeys;
import com.ct.gd.passport.view.widget.CustomHeaderFragment;
import com.umeng.analytics.MobclickAgent;

public class ConsultActivity extends BaseActivity implements OnClickListener {
	private Button		mBtnCall;
	private GridLayout	mAreaDashBoard;
	private int[]		mIcons	= { 
			R.drawable.ic_consult_cancel_passport,
			R.drawable.ic_consult_urge_certificate,
			R.drawable.ic_consult_check_out_progress1,
			R.drawable.ic_consult_import,
			R.drawable.ic_consult_check_out_progress2,
			R.drawable.ic_consult_export,
			R.drawable.ic_consult_business_certificate,
			R.drawable.ic_consult_check_out_reply,
			R.drawable.ic_consult_misc,
			};

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.consult);
	}

	@Override
	protected void initViews() {
		CustomHeaderFragment header = (CustomHeaderFragment) getFragmentManager().findFragmentById(R.id.header);
		header.setCenterText(R.string.online_advice);
		header.setRightText(R.string.advice_list_my);
		header.setRightOnClickListener(this);

		mBtnCall = (Button) findViewById(R.id.btn_consult_call);
		mBtnCall.setOnClickListener(this);

		mAreaDashBoard = (GridLayout) findViewById(R.id.area_consult_dashboard);

		mAreaDashBoard.post(new Runnable() {
			private static final int	MARGIN	= 5;

			@Override
			public void run() {
				for (AdviceForm adviceForm : AdviceForm.values()) {
					TextView v = (TextView) getLayoutInflater().inflate(R.layout.consult_dashboard_item, mAreaDashBoard, false);
					v.setCompoundDrawablesWithIntrinsicBounds(0, mIcons[adviceForm.ordinal()], 0, 0);
					v.setText(adviceForm.name);
					v.setTag(adviceForm);
					v.setOnClickListener(ConsultActivity.this);
					mAreaDashBoard.addView(v, genLayoutParam());
				}
			}

			private GridLayout.LayoutParams genLayoutParam() {
				GridLayout.LayoutParams lp;
				int columnCount = mAreaDashBoard.getColumnCount();
				int rowCount = mAreaDashBoard.getRowCount();
				int width = calc(mAreaDashBoard.getWidth(), columnCount, MARGIN);
				int height = calc(mAreaDashBoard.getHeight(), rowCount, MARGIN);
				LayoutParams wh = new ViewGroup.LayoutParams(width, height);
				lp = new GridLayout.LayoutParams(wh);
				lp.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
				return lp;
			}

			private int calc(int total, int count, int margin) {
				return (total - 2 * count * margin) / count;
			}
		});
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.tv_header_right:
			MobclickAgent.onEvent(this, UmengVar.ADVICE_MY);
			intent = new Intent(this, MyConsultActivity.class);
			break;
		case R.id.btn_consult_call:
			MobclickAgent.onEvent(this, UmengVar.ADVICE_CALL);
			intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "114"));
			break;
		default:
			intent = new Intent(this, SubmitActivity.class);
			MobclickAgent.onEvent(this, ((AdviceForm) v.getTag()).intro_event_id);
			intent.putExtra(BundleKeys.ADVICE_FORM, (AdviceForm) v.getTag());
			break;
		}
		if (null != intent) startActivity(intent);
	}

}
